angular.module('mamApp').controller('noticeCreateController', ['$scope', 'NoticeManager', 'MessageManager', '$state', '$uibModal', '$sce',
    function ($scope, NoticeManager, MessageManager, $state, $uibModal, $sce) {
        $scope.$sce = $sce;

        $scope.titleStyles = [{key: 301, value: $scope.table[301]}, {key: 302, value: $scope.table[302]}, {
            key: 303,
            value: $scope.table[303]
        }];
        $scope.Lvs = [{key: 20, value: $scope.table[20]}, {key: 21, value: $scope.table[21]}, {
            key: 22,
            value: $scope.table[22]
        }];
        $scope.signs = [{key: 30, value: $scope.table[30]}, {key: 31, value: $scope.table[31]}];
        $scope.checkStatus = [{key: 7, value: $scope.table[7]}, {key: 8, value: $scope.table[8]}, {
            key: 9,
            value: $scope.table[9]
        }];
        $scope.rangeDate = {
            startDate: $scope.search.startDay ? moment($scope.search.startDay).format('X') : null,
            endDate: $scope.search.endDay ? moment($scope.search.endDay).format('X') : null,
            onSelected: function (data, scope) {  //时间选择确认事件
                $scope.formData.startDay = data.startDate ? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD") : null;
                $scope.formData.endDay = data.endDate ? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD") : null;

            }
        };

        var noticeId = $state.params.noticeId;

        if(noticeId){
            NoticeManager.noticeDetail(noticeId).then(function (result) {
                $scope.noticeDetail = result.data.noticeVO;
                console.log($scope.noticeDetail)
            }) 
        }
       

        $scope.sendToChange = function () {

            $scope.Lvs.forEach(function (level) {
                level.checked = false;

            })
            $scope.signs.forEach(function (sign) {
                sign.checked = false;

            })
            $scope.checkStatus.forEach(function (checkStatus) {
                checkStatus.checked = false;

            })
        }

        //创建公告
        $scope.createNotice = function () {
            var titleStyleSelect = [];
            var Lvs = [];
            var signs = [];
            var statuses = [];
            $scope.titleStyles.forEach(function (titleStyle) {
                if (titleStyle.checked) {
                    titleStyleSelect.push(titleStyle.key);
                }
            })
            $scope.Lvs.forEach(function (level) {
                if (level.checked) {
                    Lvs.push(level.key);
                }
            })
            $scope.signs.forEach(function (sign) {
                if (sign.checked) {
                    signs.push(sign.key);
                }
            })
            $scope.checkStatus.forEach(function (checkStatus) {
                if (checkStatus.checked) {
                    statuses.push(checkStatus.key);
                }
            })
            if (titleStyleSelect.indexOf(301) > -1) {
                $scope.formData.sort = 1;
            } else {
                $scope.formData.sort = 2;

            }
            var label = Lvs.concat(signs).concat(statuses);
            $scope.formData.label = label.length ? label.join(",") : "all";
            $scope.formData.charMark = titleStyleSelect.join(",");
            NoticeManager.noticeAdd($scope.formData).then(function (result) {
                if (result.data.c == "s") {
                    $state.go("main.notice", {}, {reload: true})
                }
            })


        }


    }])

